<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form size="large" ref="formRef" :model="formData" :rules="formRules" label-width="100px"
      v-loading="formLoading">
      <el-form-item label="名称(英文)" prop="customoidDescr">
        <el-input v-model="formData.customoidDescr" placeholder="请输入英文名称" />
      </el-form-item>
      <el-form-item label="当前值" prop="customoidCurrent">
        <el-input v-model="formData.customoidCurrent" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="OID" prop="customoidOid">
        <el-input v-model="formData.customoidOid" placeholder="请输入OID" />
      </el-form-item>
      <el-col>
        <el-form-item label="数据类型" prop="authalgo">
          <el-select v-model="formData.customoidDatatype" placeholder="请选择" style="width: 100%">
            <el-option v-for="dict in getIntDictOptionsString(DICT_TYPE.DATA_TYPE_OID)" :key="dict.value"
              :label="dict.label" :value="dict.value" />
          </el-select>
        </el-form-item>
      </el-col>
      <el-form-item label="告警上限阈值" prop="customoidLimit">
        <el-input type="number" @wheel.prevent v-model="formData.customoidLimit" placeholder="请输入告警上限阈值" />
      </el-form-item>
      <el-form-item label="告警下限阈值" prop="customoidLimitLow">
        <el-input type="number" @wheel.prevent v-model="formData.customoidLimitLow" placeholder="请输入告警下限阈值" />
      </el-form-item>
      <el-form-item label="启用告警" prop="pass">
        <el-radio-group v-model="formData.customoidAlert" fill="#6cf">
          <el-radio-button v-for="item in getIntDictOptions(DICT_TYPE.ENABLE_ALARM)" :key="item.value"
            :value="item.value" :label="item.label" />
        </el-radio-group>
      </el-form-item>
    </el-form>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { CustomOidApi, CustomOid } from '@/api/business/customOid'
import { getIntDictOptions, getIntDictOptionsString, DICT_TYPE } from '@/utils/dict'
/** 自定义OID 表单 */
defineOptions({ name: 'CustomOidForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改
const formData = ref({
  customoidId: undefined,
  deviceId: undefined,
  customoidDescr: undefined,
  customoidDeleted: undefined,
  customoidCurrent: undefined,
  customoidPrev: undefined,
  customoidOid: undefined,
  customoidDatatype: undefined,
  customoidUnit: undefined,
  customoidDivisor: undefined,
  customoidMultiplier: undefined,
  customoidLimit: undefined,
  customoidLimitWarn: undefined,
  customoidLimitLow: undefined,
  customoidLimitLowWarn: undefined,
  customoidAlert: 1,
  customoidPassed: undefined,
  lastupdate: undefined,
  userFunc: undefined,
})
const formRules = reactive({
  customoidDescr: [{ required: true, message: '名称不能为空', trigger: 'change' }],
  customoidDatatype: [{ required: true, message: '数据类型不能为空', trigger: 'change' }],
  customoidOid: [{ required: true, message: 'OID不能为空', trigger: 'change' }],

})
const formRef = ref() // 表单 Ref
const deviceId = ref()
/** 打开弹窗 */
const open = async (type: string, id?: number, device?: number) => {
  deviceId.value = device
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  console.log("iddddd", id);

  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await CustomOidApi.getCustomOid(id)
    } finally {
      formLoading.value = false
    }
  }

}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

/** 提交表单 */
const emit = defineEmits(['success']) // 定义 success 事件，用于操作成功后的回调
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as CustomOid
    if (formType.value === 'create') {
      formData.value.deviceId = deviceId.value
      await CustomOidApi.createCustomOid(data)
      message.success(t('common.createSuccess'))
    } else {
      await CustomOidApi.updateCustomOid(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('success')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    customoidId: undefined,
    deviceId: undefined,
    customoidDescr: undefined,
    customoidDeleted: undefined,
    customoidCurrent: undefined,
    customoidPrev: undefined,
    customoidOid: undefined,
    customoidDatatype: undefined,
    customoidUnit: undefined,
    customoidDivisor: undefined,
    customoidMultiplier: undefined,
    customoidLimit: undefined,
    customoidLimitWarn: undefined,
    customoidLimitLow: undefined,
    customoidLimitLowWarn: undefined,
    customoidAlert: 1,
    customoidPassed: undefined,
    lastupdate: undefined,
    userFunc: undefined,
  }
  formRef.value?.resetFields()
}
</script>